/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.khstudio.filebrowser.util;

import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.khstudio.filebrowser.R;

public class KhDialog extends DialogFragment {
    private static final int    MODE_NONE   = 0x0000;
    private static final int    MODE_SIMPLE = 0x0001;
    private static final int    MODE_EDIT   = 0x0002;

    private int         mMode;

    private EditText    mEditText;

    private String      mTitle;
    private String      mBodyMsg;
    private String      mEditMsg;

    private boolean     mPositiveSet;
    private boolean     mNegativeSet;
    private boolean     mCancelSet;

    private String      mPositiveText;
    private String      mNegativeText;
    private String      mCancelText;

    private OnClickListener mPositiveListener;
    private OnClickListener mNegativeListener;
    private OnClickListener mCancelListener;

    public static KhDialog showDialogSimple(FragmentManager fm, String title, String msg) {
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag("khdialog");

        if (prev != null) {
            ft.remove(prev);
        }
//        ft.addToBackStack(null);

        Bundle args = new Bundle();
        args.putInt("mode", MODE_SIMPLE);
        if(title != null) {
            args.putString("title", title);
        }
        if(msg != null) {
            args.putString("bodymsg", msg);
        }

        // Create and show the dialog.
        KhDialog f = new KhDialog();
        f.setArguments(args);
        f.show(ft, "khdialog");

        return f;
    }

    public static KhDialog showDialogEdit(FragmentManager fm, String title, String bodymsg, String editmsg) {
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag("khdialog");

        if (prev != null) {
            ft.remove(prev);
        }
//        ft.addToBackStack(null);

        Bundle args = new Bundle();
        args.putInt("mode", MODE_EDIT);
        if(title != null) {
            args.putString("title", title);
        }
        if(bodymsg != null) {
            args.putString("bodymsg", bodymsg);
        }
        if(editmsg != null) {
            args.putString("editmsg", editmsg);
        }

        // Create and show the dialog.
        KhDialog f = new KhDialog();
        f.setArguments(args);
        f.show(ft, "khdialog");

        return f;
    }

    private KhDialog() {
        mMode           = MODE_NONE;

        mEditText       = null;

        mTitle          = null;
        mBodyMsg        = null;
        mEditMsg        = null;

        mPositiveSet    = false;
        mNegativeSet    = false;
        mCancelSet      = false;

        mPositiveText   = null;
        mNegativeText   = null;
        mCancelText     = null;

        mPositiveListener   = null;
        mNegativeListener   = null;
        mCancelListener     = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mMode    = getArguments().getInt("mode");
        mTitle   = getArguments().getString("title", null);
        mBodyMsg = getArguments().getString("bodymsg", null);
        mEditMsg = getArguments().getString("editmsg", null);

        if(mTitle != null) {
            setStyle(DialogFragment.STYLE_NORMAL, 0);
        } else {
            setStyle(DialogFragment.STYLE_NO_TITLE, 0);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = null;

        if(mTitle != null) {
            getDialog().setTitle(mTitle);
        }

        if(mMode == MODE_SIMPLE) {
            v = _createViewSimple(inflater, container, savedInstanceState);
        } else if(mMode == MODE_EDIT) {
            v = _createViewEdit(inflater, container, savedInstanceState);
        } else {
            return null;
        }

        return v;
    }

    public void setPositiveButton(String text, OnClickListener l) {
        mPositiveSet = true;
        mPositiveText = text;
        mPositiveListener = l;
    }

    public void setNegativeButton(String text, OnClickListener l) {
        mNegativeSet = true;
        mNegativeText = text;
        mNegativeListener = l;
    }

    public void setCancelButton(String text, OnClickListener l) {
        mCancelSet = true;
        mCancelText = text;
        mCancelListener = l;
    }

    public String getEditText() {
        if(mEditText != null) {
            return mEditText.getText().toString();
        }
        return null;
    }

    private View _createViewSimple(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.kh_dialog, container, false);
        if(mBodyMsg != null) {
            TextView tv = (TextView) v.findViewById(R.id.kh_dialog_body);
            tv.setText(mBodyMsg);
            tv.setVisibility(View.VISIBLE);
        }
        _createButton(v);

        return v;
    }

    private View _createViewEdit(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.kh_dialog, container, false);
        if(mBodyMsg != null) {
            TextView tv = (TextView) v.findViewById(R.id.kh_dialog_body);
            tv.setText(mBodyMsg);
            tv.setVisibility(View.VISIBLE);
        }

        mEditText = (EditText) v.findViewById(R.id.kh_dialog_edit);
        mEditText.setText(mEditMsg);
        mEditText.setVisibility(View.VISIBLE);

        _createButton(v);

        return v;
    }

    private void _createButton(View parent) {
        if(mPositiveSet) {
            Button b = (Button) parent.findViewById(R.id.kh_dialog_btn_positive);
            b.setText(mPositiveText);
            b.setVisibility(View.VISIBLE);
            b.setOnClickListener(mPositiveListener);
        }

        if(mNegativeSet) {
            Button b = (Button) parent.findViewById(R.id.kh_dialog_btn_negative);
            b.setText(mNegativeText);
            b.setVisibility(View.VISIBLE);
            b.setOnClickListener(mNegativeListener);
        }

        if(mCancelSet) {
            Button b = (Button) parent.findViewById(R.id.kh_dialog_btn_cancel);
            b.setText(mCancelText);
            b.setVisibility(View.VISIBLE);
            b.setOnClickListener(mCancelListener);
        }
    }

}
